$(function() {
    //alert(11)
    getAddress();
    //判断男女
    let sex = '';
    template.defaults.imports.dateFormat = function(val) {
            if (val == 0) {
                sex = '女士'
            } else if (val == 1) {
                sex = '先生'
            }
            return sex
        }
        //判断是否默认选中
    template.defaults.imports.getCheck = function(val) {
            let src = ''
            if (val === 1) {
                src = './images/checked_true.png'
            } else if (val === 0) {
                src = './images/checked_false.png'
            }
            return src
        }
        //地址背景颜色
    template.defaults.imports.getColor = function(val) {
        let Color = ""
        if (val === '公司') {
            Color = "spanCompany"
        } else if (val === '家') {
            Color = "spanHome"
        } else if (val === '学校') {
            Color = "spanSchool"
        } else if (val === '无') {
            Color = " "
        }
        return Color
    }

    function getAddress() {
        $.ajax({
            method: 'get',
            url: 'http://121.4.102.44:8083/addressBook/list',
            success(res) {
                console.log(res);
                if (res.code !== 1) {
                    return layui.layer.msg(res.msg, { icon: 5 })
                }
                const htmlStr = template('hlq_address', res)
                $('.divContent').html(htmlStr)
            }
        })
    }
    //更改默认地址
    $('.divContent').on('click', '.divDefault img', function() {
        const id = $(this).attr('data-id')
        $.ajax({
            method: 'put',
            url: 'http://121.4.102.44:8083/addressBook/default',
            data: JSON.stringify({ id }),
            contentType: "application/json",
            success(res) {
                // console.log(res);
                if (res.code !== 1) {
                    return layui.layer.msg(res.msg, { icon: 5 })
                }
                layui.layer.msg('更改默认地址成功', { icon: 6, time: 1000 })
                getAddress()
            }

        })
    })
    $('.divBottom').on('click', function() {
        location.href = './add-address.html'
    })
    $('.icon-arrow-left').on('click', function() {
        location.href = './user.html'
    })
    $('body').on('click', '.r', function() {
        const id = $(this).attr('data-id')
        localStorage.setItem('id', id)
        location.href = './edit.html'
    })
})